package com.fanrenqiji.common.utils;

import com.fanrenqiji.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.google.common.collect.Maps;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.subject.Subject;

import java.util.Map;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年4月1日 上午10:27:43
 * @description
 */
public class SystemAuthorizingCacheUtils {
	
		public static Object getCache(String key) {
			return getCache(key, null);
		}

		public static Object getCache(String key, Object defaultValue) {
			Object obj = getCacheMap().get(key);
			return obj == null ? defaultValue : obj;
		}

		public static void putCache(String key, Object value) {
			getCacheMap().put(key, value);
		}

		public static void removeCache(String key) {
			getCacheMap().remove(key);
		}

		public static Map<String, Object> getCacheMap() {
			Map<String, Object> map = Maps.newHashMap();
			try {
				Subject subject = SecurityUtils.getSubject();
				Principal principal = (Principal) subject.getPrincipal();
				return principal != null ? principal.getCacheMap() : map;
			} catch (UnavailableSecurityManagerException e) {
				return map;
			}
		}
}
